package top.asteam.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.asteam.springcloud.service.OrderService;

/**
 * @Author Rock Wang
 * @Time 2021/4/21
 */
@RestController
@RequestMapping("/order")
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod",commandProperties = {
        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "6000"),
        @HystrixProperty(name = "execution.timeout.enabled", value = "true")
})
public class OrderCotroller {

    @Autowired
    private OrderService orderService;

    @GetMapping("/payment/ok")
    public String paymentInfoOk(@RequestParam Long id) {
        return orderService.paymentInfoOk(id);
    }

//    @HystrixCommand(fallbackMethod = "paymentInfoTimeOutHandler", commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500"),
//            @HystrixProperty(name = "execution.timeout.enabled", value = "true")
//    })

    @HystrixCommand
    @GetMapping("/payment/error")
    public String paymentInfoTimeOut(@RequestParam Long id) {
        return orderService.paymentInfoTimeOut(id);
    }

    public String paymentInfoTimeOutHandler(@RequestParam Long id) {
        return "消费者80检测到paymentInfoTimeOut服务已经降级，请稍后再试";
    }


    /**
     * 下面是全局fallback方法
     *
     * @return
     */
    public String payment_Global_FallbackMethod() {
        return "Global异常处理信息，请稍后再试，/(ㄒoㄒ)/~~";
    }


}
